//
//  CYVisitorView.swift
//  WeiBo
//
//  Created by Mr_J on 16/3/4.
//  Copyright © 2016年 mac. All rights reserved.
//

import UIKit
import SnapKit


@objc protocol CYVisitorViewDelegate: NSObjectProtocol {
    
    optional func visitorView(visitorView:CYVisitorView,loginButtonDidClick:UIButton);
}

class CYVisitorView: UIView {

    weak var delegate: CYVisitorViewDelegate?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
        
        startAnimation()
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func setPageInfo(imageNamed:String?,message:String?){
        if imageNamed == nil {
            
        }else {
            circleView.hidden = true
            iconView.image = UIImage(named: imageNamed!)
            messageLabel.text = message
            
        }
 
    }
    
    private func startAnimation() {
        let anim = CABasicAnimation(keyPath: "transform.rotation")
        
        anim.toValue = 2 * M_PI
        
        anim.repeatCount = MAXFLOAT
        
        anim.duration = 20
        
        anim.removedOnCompletion = false
        
        circleView.layer.addAnimation(anim, forKey: nil)
    }
//    @objc private func loginButtonClick(button:UIButton) {
//        delegate?.visitorView?(self, loginButtonDidClick: button)
//    }
    
    private lazy var iconView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_house"))
    
    private lazy var circleView:UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_image_smallicon"))
    
    private lazy var messageLabel:UILabel = {
       
        let label = UILabel(textColor: UIColor.darkGrayColor(), fontSize: 14)
        
        label.numberOfLines = 0

        label.textAlignment = .Center
        
        label.text = "关注一些人，回这里看看有什么惊喜"
        
        return label
    }()
    
    lazy var registerButton: UIButton = {
        let button = UIButton(textColor: UIColor.orangeColor(), fontSizr: 14)
       
//        button.addTarget(self, action: "", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("注册", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        
        return button
        
    }()
    
    lazy var loginButton: UIButton = {
        let button = UIButton(textColor: UIColor.darkGrayColor(), fontSizr: 14)
        
//        button.addTarget(self, action: "login", forControlEvents: UIControlEvents.TouchUpInside)
        
        button.setTitle("登陆", forState: UIControlState.Normal)
        button.setBackgroundImage(UIImage(named: "common_button_white"), forState: UIControlState.Normal)
        
        return button
    }()
    
    private lazy var maskIconView: UIImageView = UIImageView(image: UIImage(named: "visitordiscover_feed_mask_smallicon"))
    
}

extension CYVisitorView {
    
    private func setupUI() {
        backgroundColor = UIColor(white: 237/255, alpha: 1)
        
        addSubview(circleView)
        addSubview(maskIconView)
        addSubview(iconView)
        addSubview(messageLabel)
        addSubview(registerButton)
        addSubview(loginButton)
        
        
        iconView.translatesAutoresizingMaskIntoConstraints = false
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterX, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterX, multiplier: 1, constant: 0))
        addConstraint(NSLayoutConstraint(item: iconView, attribute: NSLayoutAttribute.CenterY, relatedBy: NSLayoutRelation.Equal, toItem: self, attribute: NSLayoutAttribute.CenterY, multiplier: 1, constant: 0))
        
        
        circleView.snp_makeConstraints { (make) -> Void in
            make.center.equalTo(iconView)
        }
        
        messageLabel.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(iconView)
            make.top.equalTo(circleView.snp_bottom)
            make.width.equalTo(224)
            
        }
        
        registerButton.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(messageLabel)
            make.top.equalTo(messageLabel.snp_bottom)
            make.width.equalTo(100)
        }
        
        loginButton.snp_makeConstraints { (make) -> Void in
            make.trailing.equalTo(messageLabel)
            make.top.equalTo(registerButton)
            make.width.equalTo(100)
            
        }
        
        maskIconView.snp_makeConstraints { (make) -> Void in
//            make.center.equalTo(iconView)
//            make.height.equalTo(50)
            make.centerX.equalTo(circleView)
            make.bottom.equalTo(circleView)
            make.height.equalTo(300)
            
        }
        
    }
 
}

